<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>get</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const get = (from, ...selectors) =>
            [...selectors]
                .filter(s => typeof s === 'string')
                .map(s =>
                    s
                        .replace(/\[([^\[\]]*)\]/g, '.$1.')
                        .split('.')
                        .filter(t => t !== '')
                        .reduce((prev, cur) => prev && prev[cur], from)
                );
        const obj = {
            selector: { to: { val: 'val to select' } },
            target: [1, 2, { a: 'test' }]
        };
        console.log(get(obj, 'selector.to.val', 'target[0]', 'target[2].a'));;
    </script>
</body>

</html>